package test.column
{

import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;

import com.fit4as.fit.Parse;
import com.fit4as.fit.ParseException;
import com.fit4as.fit.Fixture;

public class ColumnFixtureTest extends TestCase
{

    public function ColumnFixtureTest(methodName:String) {
        super(methodName);
    }

    public static function suite():TestSuite {
        var ts:TestSuite = new TestSuite();
        ts.addTest(new ColumnFixtureTest("shouldSetAProperty"));
        ts.addTest(new ColumnFixtureTest("shouldSetTwoPropertyInGivenOrder"));
        ts.addTest(new ColumnFixtureTest("shouldCallAMethod"));
        ts.addTest(new ColumnFixtureTest("shouldUseCorrectType"));
        return ts;
    }
    
    private var fixture : Fixture;
    
    override public function setUp() : void
    {
    	fixture = new Fixture();	
    }
    
    [test]
    public function shouldSetAProperty() : void
    {
    	var table : Parse = Parse.createParse(
	    	"<table>"
	    	+ "<tr><td>test.column.TestColumnFixture</td></tr>"
	    	+ "<tr><td>property</td></tr>"
	    	+ "<tr><td>value</td></tr>"
	    	+ "</table>");
	    	
	    var recordedSetterCalls : Array = test.column.TestColumnFixture.recordedSetterCalls = new Array();	
	    fixture.doTables( table );	
	    
	    assertEquals( 1, recordedSetterCalls.length );
	    assertEquals( "set property", recordedSetterCalls[ 0 ].name );
	    assertEquals( "value", recordedSetterCalls[ 0 ].value );
    }
    
    [test]
    public function shouldSetTwoPropertyInGivenOrder() : void
    {
    	var table : Parse = Parse.createParse(
	    	"<table>"
	    	+ "<tr><td>test.column.TestColumnFixture</td></tr>"
	    	+ "<tr><td>property</td><td>secondProperty</td></tr>"
	    	+ "<tr><td>value</td><td>secondValue</td></tr>"
	    	+ "</table>");
	    	
	    var recordedSetterCalls : Array = test.column.TestColumnFixture.recordedSetterCalls = new Array();	
	    fixture.doTables( table );	
	    
	    assertEquals( 2, recordedSetterCalls.length );
	    assertEquals( "set property", recordedSetterCalls[ 0 ].name );
	    assertEquals( "value", recordedSetterCalls[ 0 ].value );
	    assertEquals( "set secondProperty", recordedSetterCalls[ 1 ].name );
	    assertEquals( "secondValue", recordedSetterCalls[ 1 ].value );
    }

	[test]
    public function shouldCallAMethod() : void
    {
    	var table : Parse = Parse.createParse(
	    	"<table>"
	    	+ "<tr><td>test.column.TestColumnFixture</td></tr>"
	    	+ "<tr><td>method()</td></tr>"
	    	+ "<tr><td>expectedResult</td></tr>"
	    	+ "</table>");
	    	
	    var recordedMethodCalls : Array = test.column.TestColumnFixture.recordedMethodCalls = new Array();	
	    fixture.doTables( table );	
	    
	    assertEquals( 1, recordedMethodCalls.length );
	    assertEquals( "method", recordedMethodCalls[ 0 ] );
    }

	[test]
	public function shouldUseCorrectType() : void
	{
		var assertForProperty : Function = function( property : String, value : String ) : void
			{
				var table : Parse = Parse.createParse(
			    	"<table>"
			    	+ "<tr><td>test.column.TestColumnFixture</td></tr>"
			    	+ "<tr><td>" + property + "</td></tr>"
			    	+ "<tr><td>" + value + "</td></tr>"
			    	+ "</table>");
			    fixture.doTables( table );	
				assertEquals( "error when using " + property, 0, fixture.counts.exceptions );	    
			}
		//string
		assertForProperty( "stringProperty", "stringValue" );

		//int
		assertForProperty( "intProperty", "7" );
		
		//Number
		assertForProperty( "numberProperty", "7.5" ); 

		//Array
		assertForProperty( "arrayProperty", "1,2,3" );

		//custom object
		assertForProperty( "customObjectProperty", "id" );
	}
}
}